package test.data_structure.linked_list;

public class LinkedList<T> {

    private class Node {

        private T data;

        private Node next;

        public Node(T e, Node next) {
            this.data = e;
            this.next = next;
        }

        public Node(T e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return data.toString();
        }
    }

    private Node head;

    private int size;

    public LinkedList() {
        head = new Node();
        size = 0;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void add(int index, T e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("index is invalid.");
        }

        Node prev = head;
        for (int i = 0 ; i < index ; i++) {
            prev = prev.next;
        }
        prev.next = new Node(e, prev.next);
        size ++;
    }

    public void addFirst(T e) {
        add(0, e);
    }

    public void addLast(T e) {
        add(size, e);
    }

    public T get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("index is invalid.");
        }

        Node p = head.next;
        for (int i = 0 ; i < index ; i ++) {
            p = p.next;
        }
        return p.data;
    }

    public T getFirst() {
        return get(0);
    }

    public T getLast() {
        return get(size - 1);
    }

    public void set(int index, T e) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("index is invalid.");
        }
        Node p = head.next;
        for (int i = 0 ; i < index; i++) {
            p = p.next;
        }
        p.data = e;
    }

    public boolean contains(T e) {
        Node p = head.next;
        while(p.next != null) {
            if (p.data.equals(e)) {
                return true;
            }
            p = p.next;
        }
        return false;
    }

    public T remove(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("index is not valid.");
        }
        Node p = head;
        for (int i = 0 ; i < index ; i ++) {
            p = p.next;
        }
        Node d = p.next;
        p.next = d.next;
        d.next = null;
        size --;
        return d.data;
    }

    public T removeFirst() {
        return remove(0);
    }

    public T removeLast() {
        return remove(size - 1);
    }

    public void removeElement(T e) {
        Node p = head;
        while (p.next != null) {
            if (p.next.data.equals(e)) {
                break;
            }
            p = p.next;
        }
        if (p.next != null) {
            Node del = p.next;
            p.next = del.next;
            del.next = null;
            size --;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Linked list:");
        Node p = head.next;
        while (p != null) {
            sb.append(p + "->");
            p = p.next;
        }
        sb.append("NULL");
        return sb.toString();
    }

    public static void main(String[] args) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        for (int i = 0 ; i < 10 ; i ++) {
            linkedList.addFirst(i);
            System.out.println(linkedList);
        }
        linkedList.add(2, 23);
        System.out.println(linkedList);

        linkedList.removeFirst();
        System.out.println(linkedList);
        linkedList.removeLast();
        System.out.println(linkedList);
        linkedList.removeElement(2);
        System.out.println(linkedList);
    }

}
